package com.zj.springbootmybatisannotation.controller;

import com.zj.springbootmybatisannotation.model.User;
import com.zj.springbootmybatisannotation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: zj
 * @Date: 2019/3/3 20:03
 * @Description:
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public int addUser(User user){
        return userService.addUser(user);
    }

    @GetMapping("/all")
    public Object findAllUser(
            @RequestParam(name = "pageNum", required = false, defaultValue = "1")
                    int pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                    int pageSize){
        return userService.findAllUser(pageNum,pageSize);
    }

    @GetMapping("/test")
    @Transactional
    public Object test1111111() throws Exception{
                User user = new User();
                user.setUsername( "xxxx" );
                user.setAge( 18 );
                userService.addUser( user );

                int i=1/0;

                User user1 = new User();
                user1.setUsername( "yyyy" );
                user1.setAge( 22 );
                userService.addUser( user1 );
                return "插入成功";
    }

}
